<?php
namespace app\model\product;

use think\Model;

class ZxlKnowStoreProductModel extends Model
{
    protected $name = 'zxl_know_store_product';

    const PC_PRODUCTTYPE = [1,3,5,6,10]; // PC端固定产品类型
    
    /**
     * 商品类型
     */
    public static function getPcProductType($key=''){
        $type = [
            '1' => '实物',
            '3' => '课程',
            '5' => '题库',
            '6' => '直播课',
            '10' => '考试',
        ];
        if('' !== $key){
            if(array_key_exists($key,$type)){
                return $type[$key];
            }
            return false;
        }
        return $type;
    }

    /**
     * 商品详情页
     * 
     * @return string
     */
    public static function getPcProductDetailHref($key, $id){
        $type = [
            '1' => '/goods/detail?id='.$id,
            '3' => '/course/detail?id='.$id,
            '5' => '/question/chapter_detail?id='.$id,
            '6' => '/live/detail?id='.$id,
            '10' => '/exam/detail?id='.$id,
        ];
        if('' !== $key){
            if(array_key_exists($key,$type)){
                return $type[$key];
            }
            return false;
        }
        return $type;
    }

    /**
     * 获取最低价格规格
     * 
     * @return string
     */
    public function attrMinPrice(){
        return $this->hasOne(ZxlKnowStoreProductAttrModel::class, "product_id", "id")->where(["is_delete" => 0, 'status' => 1])->order('sale_price', 'asc')->bind(['sale_price','original_price', 'attr_id' => 'id']);
    }

    /**
     * 获取商品所有规格
     * 
     * @return array
     */
    public function specs(){
        return $this->hasMany(ZxlKnowStoreProductAttrModel::class, "product_id", "id")
        ->order(['is_default'=>'desc','create_time'=>'asc']);
    }

    /**
     * 获取标签
     * 
     * @return string
     */
    public static function get_tag_list($tags)
    {
        $tag_list = json_decode($tags, true);
        $tags = '';
        foreach ($tag_list as $val) {
            if ($val) {
                $tags .= '<span class="layui-btn layui-btn-primary layui-btn-xs">'.$val.'</span>';
            }
        }
        return $tags;
    }

    /**
     * 班型
     */
    public static function get_max_users($key=''){
        $type = [
            '1' => '1 V 1',
            '6' => '1 V 6',
            '12' => '1 V 12',
            '0' => '大班',
        ];
        if('' !== $key){
            if(array_key_exists($key,$type)){
                return $type[$key];
            }
            return false;
        }
        return $type;
    }

    /**
     * 获取授权商品主商品ID
     *
     * @param $product_id
     * @return int
     */
    public static function getMainProductId($product_id){
        $info = ZxlKnowStoreProductModel::where(["id"=>$product_id])->find();
        return $info['p_product_id'] ? $info['p_product_id'] : $info['id'];
    }

    /**
     * 获取商品详情
     * 
     * @return array
     */
    public static function getInfo($m_id, $product_id, $is_buy = '')
    {
        $where = function($q)use($m_id, $product_id, $is_buy){
            $q->where('m_id', $m_id);
            $q->where('id', $product_id);
            if ($is_buy != '') {
                if ($is_buy == 0) { // 用户没有购买需要验证商品是否删除 0未购买 1已购买
                    $q->where('is_delete', 0);
                }
            }
        };
        $info = ZxlKnowStoreProductModel::with("attrMinPrice")->where($where)->find();
        return $info;
    }
}